//
//  YMUser.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/3/28.
//

import UIKit
import HandyJSON

class YMUser: HandyJSON {
    var userId :String = ""
    var token  :String = ""
    var name   :String = ""
    var phone  :String = ""
    var corver :String = ""
    
    var nickName :String{
        return YMUser.loginIn ? moya.user.name : "请登录"
    }
    required public init() {}
}
extension YMUser{
    static var loginIn :Bool{
        return moya.user.name.count > 0 ? true : false
    }
    static var user: YMUser = {
        let data = UserDefaults.standard.object(forKey:moya.defaultLogin)
        let json = JSON(data as Any)
        guard let info = YMUser.deserialize(from: json.rawString()) else { return YMUser() }
        return info
    }()
    //MARK:保存信息
    static func saveUser(_ user :YMUser){
        moya.user = user
        let defaults = UserDefaults.standard
        guard let dic = user.toJSON() else { return }
        defaults.set(dic, forKey: moya.defaultLogin)
        defaults.synchronize()
    }
    //MARK:删除信息
    static func deleteUser(){
        let defaults = UserDefaults.standard
        defaults.removeObject(forKey: moya.defaultLogin)
        defaults.synchronize()
        moya.user = YMUser()
    }
}
